/*----------------------------------------------------------------------------
 * Copyright (c) <2019-2020>, <Huawei Technologies Co., Ltd>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *---------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
 * Notice of Export Control Law
 * ===============================================
 * Huawei OMIP(belong to OMSIS) may be subject to applicable export control laws and regulations, which might
 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
 * applicable export control laws and regulations.
 *---------------------------------------------------------------------------*/

#ifndef _OMIP_LOG_H_
#define _OMIP_LOG_H_

#include <stdio.h>
#include "zone.h"

#define OMIP_DEBUG_INFO_LEN             256
#define OMIP_SECTOR_NOT_FULL            0
#define OMIP_SECTOR_FULL                1
#define OMIP_LOG_BUFFER_MAX_NUMBER      8
#define OMIP_FLASH_SECTOR_SIZE          ((uint32_t)0x1000)
#define OMIP_FLASH_SECTOR_MASK          (OMIP_FLASH_SECTOR_SIZE - 1)  /* 0000 0000 0000 0000 0000 1111 1111 1111 */
#define OMIP_VALID_LOG_FLAG             0x2110b030 // 21.1.T1.B030

#define OMIP_LOG_DIVIDE_LINE     "----------------------------------------------------------------------------------\n"
#define OMIP_LOG_FROM_CACHE_LINE "--------------------------------GET LOG FROM CACHE--------------------------------\n"
#define OMIP_LOG_FROM_FLASH_LINE "--------------------------------GET LOG FROM FLASH--------------------------------\n"



typedef struct{
    zone_id id;
    uint8_t * start_address;
	uint8_t * last_address;
    uint8_t * end_address;
}cache_log_info;


typedef struct {
    unsigned int flag;
    unsigned short logsize;
    unsigned short status;
} omip_log_sector_head;



typedef enum {
    OMIP_EMGENCY,
    OMIP_ERROR_UPLOAD,
    OMIP_ERROR_FLASH,
    OMIP_ERROR,
    OMIP_INFO_UPLOAD,
    OMIP_INFO_FLASH,
    OMIP_INFO,
    OMIP_DEBUG_UPLOAD,
    OMIP_DEBUG,
    OMIP_DETAIL,
    OMIP_MAX,
}omip_log_level;


typedef enum {
    OMIP_LOG_RECORD_CACHE,
    OMIP_LOG_RECODE_FLASH,
} omip_log_record_type;

typedef struct omip_log_level_fmt {
    omip_log_level level;
    char *fmt_str;
    char *ori_str;
} omip_log_level_fmt;

typedef enum {
    OMIP_UPLOAD_GATEWAY = 0, // device to gateway
    OMIP_UPLOAD_APP, // device to telephone app
    OMIP_UPLOAD_MAX
} omip_log_upload_direction;

typedef enum {
    OMIP_LOG_UPLOAD_OFF = 0,
    OMIP_LOG_UPLOAD_ON,
} omip_log_upload_switch;

typedef struct omip_log_upload_info {
    omip_log_level upload_log_level;
    omip_log_upload_switch upload_log_switch;
} omip_log_upload_info_para;


#define OMIP_PRINT_BUF 512 
#define OMIP_LOG_DEBUG(fmt, ...)		omip_printf(OMIP_DEBUG_UPLOAD, fmt"\r\n", ##__VA_ARGS__)
#define OMIP_LOG_INFO(fmt, ...)			omip_printf(OMIP_INFO_UPLOAD, fmt"\r\n", ##__VA_ARGS__)
#define OMIP_LOG_INFO_FLASH(fmt, ...)	omip_printf(OMIP_INFO_FLASH, fmt"\r\n", ##__VA_ARGS__)
#define OMIP_LOG_ERROR(fmt, ...)		omip_printf(OMIP_ERROR_UPLOAD, fmt"\r\n", ##__VA_ARGS__)
#define OMIP_LOG_ERROR_FLASH(fmt, ...)	omip_printf(OMIP_ERROR_FLASH, fmt"\r\n", ##__VA_ARGS__)

void omip_my_debug(char *str);//defined by user,such in main.c
void omip_printf(omip_log_level level, const char *format, ...);
void omip_print_io_bytes(const unsigned char *pp, unsigned short len, unsigned char sr, unsigned char node_id);
void omip_set_log_level(uint8_t level);
void mcu_set_time_by_sec(unsigned long long sec);
void omip_device_log_upload_app(const char *logBuffer);
void omip_set_log_switch(omip_log_upload_switch status, omip_log_upload_direction direction);
int omip_log_get_level_by_string(const char *level_name, omip_log_level *level);
void omip_set_upload_log_level(omip_log_level level, omip_log_upload_direction direction);
omip_log_upload_switch omip_get_log_switch(omip_log_upload_direction direction);
void omip_log_buffer_init(void);
int omip_read_by_type(zone_id id, unsigned int address, uint8_t* buffer, unsigned int length,
                      omip_log_record_type type);
zone_info *omip_get_zone_by_type(zone_id id, omip_log_record_type type);
int omip_log_get_head(uint32_t head_addr, omip_log_sector_head *head, omip_log_record_type type);
cache_log_info * get_cache_log_info();
int omip_read_cache_log(uint8_t *buff, uint8_t *addr, uint16_t len);
char *omip_get_level_str(omip_log_level level);
omip_log_level omip_get_log_level(void);

#if 0

typedef enum {
    LOGRECORD_ZONE = 0,
} zone_id;

typedef struct{
    zone_id id;
    unsigned int start_address;
    unsigned int end_address;
}zone_info;


#endif

#endif /* _OMIP_LOG_H_ */
